﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using DAO;

namespace QLDaiLy
{
    public partial class TinNhanVienNangCao :XtraUserControl
    {
        public TinNhanVienNangCao()
        {
            InitializeComponent();
            NamLamViec();
            TuoiNhanVien();
            LoadDanhSachChucVu();
        }
        private void LoadDanhSachChucVu()
        {
            List<ChucVuDTO> list = new List<ChucVuDTO>();
            list = ChucVuDAO.LoadChucVu();
            lookupChucVu.Properties.DataSource = list;
            lookupChucVu.Properties.ValueMember = "MaChucVu";
            lookupChucVu.Properties.DisplayMember = "TenChucVu";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo colTenLoaiDaiLy;
            colTenLoaiDaiLy = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("TenChucVu");
            colTenLoaiDaiLy.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
            lookupChucVu.Properties.Columns.Add(colTenLoaiDaiLy);
        }
        /*
        private void LoadDanhSachChucVu()
        {

            List<ChucVuDTO> list = new List<ChucVuDTO>();
            list = ChucVuDAO.LoadChucVu();
            foreach (var chucvu in list)
            {
                comboBoxEdit1.Properties.Items.Add(chucvu.);
            }
        
        }*/
        private void NamLamViec()
        {
           
            for (int i = 0; i <= 30; i++)
            {
                cbbNamLamViec.Properties.Items.Add(i);
                cbbNamLamViecDen.Properties.Items.Add(i);
            }
            cbbNamLamViec.SelectedIndex = -1;
            cbbNamLamViecDen.SelectedIndex = -1;
            cbbNamLamViec.Properties.Sorted = true;
        }
        private void TuoiNhanVien()
        {
            // int year = DateTime.Now.Year;
            for (int i = 18; i <= 60; i++)
            {
                cbbTuoiTu.Properties.Items.Add(i);
                cbbTuoiDen.Properties.Items.Add(i);
            }
            cbbTuoiTu.SelectedIndex = -1;
            cbbTuoiDen.SelectedIndex = -1;
            cbbTuoiTu.Properties.Sorted = true;
        }
        private void TimKiem()
        {
            try
            {
                int tuoiTu, tuoiDen, namLamViecTu, namLamViecDen;
                tuoiTu = tuoiDen = namLamViecTu = namLamViecDen = -500;
                NhanVienDTO dl = new NhanVienDTO();
                dl.TenNhanVien = txtTenNhanVien.Text;
                dl.DiaChi = txtDiaChi.Text;
                dl.EmailNV = txtMail.Text;
                
                if(lookupChucVu.EditValue.GetHashCode()!=-1)
                {
                    dl.MaChuVu = lookupChucVu.EditValue.GetHashCode();
                }
                if (cbbTuoiTu.SelectedIndex != -1)
                {
                    tuoiTu = cbbTuoiTu.SelectedItem.GetHashCode();
                }
                if (cbbTuoiDen.SelectedIndex != -1)
                {
                    tuoiDen = cbbTuoiDen.SelectedItem.GetHashCode();
                }
                if (cbbNamLamViec.SelectedIndex != -1)
                {
                    namLamViecTu = cbbNamLamViec.SelectedItem.GetHashCode();
                    // thangTu =int.Parse(cbbThangTu.Text.ToString());
                }
                if (cbbNamLamViecDen.SelectedIndex != -1)
                {
                    namLamViecDen = cbbNamLamViecDen.SelectedItem.GetHashCode();
                }
                DataTable dt = NhanVienDAO.TimKiemNangCao(dl, tuoiTu, tuoiDen, namLamViecTu, namLamViecDen);
               // dt = DaiLyDAO.TimKiemNangCao(dl, namTu, namDen, thangTu, thangDen, noTu, noDen);
                if (dt == null)
                {
                    lbmessenger.Text = "Không tìm thấy";
                }
                gridNhanVienControl.DataSource = dt;
            }
            catch (Exception)
            {
                lbmessenger.Text = "Không tìm thấy";
            }
        }
        private void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        private void repositoryItemPictureCapNhat_Click(object sender, EventArgs e)
        {
            string MaNhanVien = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["MaNhanVien"]).ToString();
            string TenNhanVien = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["TenNhanVien"]).ToString();
            string DienThoai = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["DienThoai"]).ToString();
            string DiaChi = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["DiaChi"]).ToString();
            string Email = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["Email"]).ToString();
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            CapNhatNhanVien frm = new CapNhatNhanVien(MaNhanVien, TenNhanVien, DiaChi, Email, DienThoai);
            tb.Controls.Add(frm, 0, 1);
        }

        private void repositoryItemPicturePhanQuyen_Click(object sender, EventArgs e)
        {
            string MaNhanVien = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["MaNhanVien"]).ToString();
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            PhanQuyen frm = new PhanQuyen(MaNhanVien);
            tb.Controls.Add(frm, 0, 1);
        }

        private void repositoryItemPictureDelete_Click(object sender, EventArgs e)
        {

            DialogResult dr = MessageBox.Show("Bạn có muốn xóa nhân viên này không?", "Thoát chương trình", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                NhanVienDTO nv = new NhanVienDTO();
                nv.MaNhanVien = gridViewDanhSachNhanVien.GetRowCellValue(gridViewDanhSachNhanVien.FocusedRowHandle, gridViewDanhSachNhanVien.Columns["MaNhanVien"]).ToString();
                if (NhanVienDAO.deleteNhanVien(nv))
                {
                    // MessageBox.Show("Xoá thành công");
                    TimKiem();
                }
                else
                {
                    MessageBox.Show("Xoá thành thất bại");
                }
            }
        }

        private void gridViewDanhSachNhanVien_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
            {
                e.Value = QLDaiLy.Properties.Resources.deletexoagia;
            }

            if (e.Column.Caption == "Cập Nhật")
            {
                e.Value = QLDaiLy.Properties.Resources.add_user;
            }
            if (e.Column.Caption == "Phân Quyền")
            {
                e.Value = QLDaiLy.Properties.Resources.updateUser;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
